yjx.controller('bannerIntroController', ['$rootScope', '$scope', '$fetch', '$stateParams', 'modalService', '$timeout', '$state', 'wxShareService','ipCookie', function ($rootScope, $scope, $fetch, $stateParams, modalService, $timeout, $state, wxShareService,ipCookie) {
    var id = $stateParams.id,
        pendingEventId = $stateParams.pendingEventId;
    $scope.forwardId = $stateParams.forwardId;
    var productStr = '';
    /*转发成功后的回调*/
    // $scope.callback = function () {
    //     /*完成待办事项*/
    //     if ($rootScope.identity == 1 && pendingEventId) {
    //         //alert(pendingEventId)
    //         $fetch.fetch(
    //             '/share/recordRelay/content/times',
    //             {
    //                 params: {
    //                     userId: $rootScope.userId,
    //                     contentId: id,
    //                     pendingEventId: pendingEventId,
    //                     type: 3
    //                 },
    //                 cache: false,
    //                 //type:'POST'
    //             }
    //         ).then(function (data) {
    //             }).catch(function (e) {
    //                 modalService.mainMsgError(e.msg)
    //             })
    //     }
    // }

    /*获取活动详情 */
    function getDetail() {
        $fetch.fetch(
            '/activity/getBanners',
            {
                params: {
                    id: id
                },
            }
        ).then(function (data) {
            $timeout(function () {
                if (data && data.banners && data.banners.length > 0) {
                    $scope.data = data.banners[0];
                    if ($scope.data.productList && $scope.data.productList.length > 0) {
                        for (var i = 0; i < $scope.data.productList.length; i++) {
                            if (i != $scope.data.productList.length - 1) {
                                productStr = productStr + $scope.data.productList[i].id + ','
                            } else {
                                productStr = productStr + $scope.data.productList[i].id
                            }

                        }
                    }
                    initWXShare();
                } else {
                    modalService.mainMsgError("数据库中不存在该活动介绍");
                }
            })
        }
            ).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
    }

    if (id) {
        getDetail();
    }
    /**微信转发 */
    function initWXShare() {
        var wxDesc = $scope.data.name;
        if ($rootScope.isAndroid) {
            var linkhead = APPSRC
        } else {
            var linkhead = APPSRC + '#/'
        }
        if ($rootScope.identity == 1) {
            // if ($rootScope.isAndroid) {
            //     $scope.link = linkhead + "?url=bannerDetail&id=" + id + "&forwardId=" + $rootScope.userId + "&pendingEventId=" + pendingEventId;

            // } else {
            //     $scope.link = linkhead + "bannerDetail?id=" + id + "&forwardId=" + $rootScope.userId + "&pendingEventId=" + pendingEventId;
            // }
            $scope.link = linkhead + "bannerDetail?id=" + id + "&forwardId=" + $rootScope.userId + "&pendingEventId=" + pendingEventId;

        } else {
            // if ($rootScope.isAndroid) {
            //     $scope.link = linkhead + "?url=bannerDetail&id=" + id;
            // } else {
            //     $scope.link = linkhead + "bannerDetail?id=" + id;
            // }
            $scope.link = linkhead + "bannerDetail?id=" + id;
        }
        wxShareService.onMenuShareAppMessage('', wxDesc, '', $scope.link, '');
        wxShareService.onMenuShareTimeline('', wxDesc, '', $scope.link, '')
    }

    /*获取转发人信息*/
    if ($scope.forwardId) {
        $fetch.fetch(
            '/userInfo/getById',
            {
                params: {
                    userId: $scope.forwardId
                }
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.forward = {
                    id: data.user.id,
                    avatar: data.user.avatar,
                    nickname: data.user.realname ? data.user.realname : data.user.nickname,
                }
            })
        }).catch(function (e) {
        })
    }
    /**客服 */
    $scope.chat = function () {
        // ipCookie.remove('productList', ipCookiesParam);
        var productList = [];
        for(var i=0;i<$scope.data.productList.length;i++){
            var product = {
                id: $scope.data.productList[i].id,
                name: $scope.data.productList[i].title,
                describe: $scope.data.productList[i].describe,
                price: $scope.data.productList[i].activityPrice,
                originalPrice: $scope.data.productList[i].price,
                supplierAvatar: $scope.data.productList[i].supplierAvatar,
            }
            productList.push(product);
        }
        ipCookie('productList',productList, ipCookiesParam);
        $state.go('chat');
    }
    /**下单预约等更多操作 */
    $scope.showMoreActs = function () {
        if ($rootScope.identity == 1) {
            modalService.actsModals();
        } else {
            $scope.goOrder();
        }

    }
    $scope.itemList = [
        { id: 1, name: '下单', callback: $scope.goOrder },
        { id: 2, name: '预约', callback: goReservation },
    ]
    $scope.goOrder = function () {
        // close();
        $state.go('shopcart.orderPay', { forwardId: $scope.forwardId, productIds: productStr })
    }
    function goReservation() {
        close();
        $state.go('reservation', { id: productStr })
    }
    /**加入购物车 */
    $scope.addShopcart = function () {
        for (var i = 0; i < $scope.data.productList.length; i++) {
            addGoods($scope.data.productList[i].id)
        }
        modalService.mainMsgOk("该产品已加入购物车")//待后端改接口
    }

    function addGoods(id) {
        $fetch.fetch(
            '/shopping/goods/add',
            {
                params: {
                    productId: id
                },
                type: 'post'
            }
        ).then(function (data) {
            // modalService.mainMsgOk("该产品已加入购物车");
        }).catch(function (e) {
            if (e.code == 2002) {
                // modalService.mainMsgOk("该产品已加入购物车");
            } else {
                modalService.mainMsgError(e.msg);
            }
        })
    }

    $scope.close = function () {
        close();
    }
    function close() {
        $rootScope.modalStatus = 'modal-hide';
        $rootScope.modalTag = "actsModals";
    }
}])